package com.ly.controller;

import com.ly.entities.CommonResult;
import com.ly.entities.Payment;
import com.ly.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 流程：
 * 1.建module
 * 2.改pom
 * 3.写yml
 * 4.主启动
 * 5.业务类
 */
@RestController()
@Slf4j
@RequestMapping("/payment")
public class PaymentController {

    //@Autowired//也可以使用，但是由于出现波浪线，因人而异，个人喜爱resource
    @Resource
    private PaymentService paymentService;

    //方便肉眼直接查看负载机制
    @Value("${server.port}")
    private String serverPort;

    /**
     * 增加方法
     * @param payment
     * @return
     */
    @PostMapping("create")
    public CommonResult create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("插入成功的条数 {}", result);
        if (result>0) {
            return  new CommonResult(200, "插入成功，serverPort："+serverPort);
        }else {
            return  new CommonResult(101, "插入失败");
        }
    }


    @GetMapping("get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") String id) {
        Payment paymentInfo = paymentService.getPaymentById(id);
        if (paymentInfo != null) {
            return  new CommonResult(200, "查询成功，serverPort"+serverPort, paymentInfo);
        }else {
            return  new CommonResult(101, "没有对应记录");
        }
    }
}
